<template>
<!--  其他费用模块 -->
  <el-form
    ref="formOtherCostRef"
    :model="formOtherCostData"
    :rules="formOtherCostRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
    :disabled="disabled"
  >
    <el-table :data="formOtherCostData" show-summary :summary-method="getSummaries" border  class="-mt-10px">
      <el-table-column label="费用名称" align="center" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.name`" class="mb-0px!">
            <el-input v-model="row.name" placeholder="请输入费用名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="金额" prop="cost" align="center"    min-width="120">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.cost`" class="mb-0px!">
            <el-input-number
              placeholder="请输入费用金额"
              v-model="row.cost"
              controls-position="right"
              :min="0"
              :precision="0"
              class="!w-100%"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center"  min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3" v-if="!disabled">
    <el-button @click="handleOtherCostAdd" round>+ 添加费用</el-button>
  </el-row>
</template>
<script setup lang="ts">

import {erpCountInputFormatter, erpPriceInputFormatter, getSumValue} from "@/utils";

const props = defineProps<{
  items: undefined
  disabled: false
}>()
const formLoading = ref(false) // 表单的加载中
const formOtherCostData = ref([])
const formOtherCostRules = reactive({
  productId: [{ required: true, message: '产品不能为空', trigger: 'blur' }],
  count: [{ required: true, message: '产品数量不能为空', trigger: 'blur' }]
})

const formOtherCostRef = ref([]) // 表单 Ref

if (watch) {
  /** 初始化设置 */
  watch(
    () => props.items,
    async (val) => {
      formOtherCostData.value = val
    },
    {immediate: true}
  )
}

/** 新增按钮操作 */
const handleOtherCostAdd = () => {
  const row = {
    name: undefined,
    cost: 0,
    remark: undefined
  }
  formOtherCostData.value.push(row)
}

/** 合计 */
const getSummaries = (param) => {
  const { columns, data } = param
  const sums: string[] = []
  columns.forEach((column, index: number) => {
    if (index === 0) {
      sums[index] = '其他费用合计'
      return
    }
    if (['cost'].includes(column.property)) {
      console.log("cost")
      const sum = getSumValue(data.map((item) => Number(item[column.property])))
      sums[index] =
        column.property === 'cost' ? erpCountInputFormatter(sum) : erpPriceInputFormatter(sum)
    } else {
      sums[index] = ''
    }
  })

  return sums
}

/** 删除按钮操作 */
const handleDelete = (index: number) => {
  formOtherCostData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formOtherCostRef.value.validate()
}

defineExpose({ validate })

if (onMounted) {
  /** 初始化 */
  onMounted(async () => {
    // 默认添加一个
    if (formOtherCostData.value === null){
      formOtherCostData.value = []
    }
    if (formOtherCostData.value.length === 0) {
      handleOtherCostAdd()
    }
  })
}
</script>
